

//
//  NSerchZhiweiViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/28.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NSerchZhiweiViewController.h"
#import "NMineCell.h"
#import "NPostionSearchResultVC.h"
#import "GFAddressPicker.h"
#import "PickerView.h"
@interface NSerchZhiweiViewController ()<UITableViewDataSource,UITableViewDelegate,STPickerSingleDelegate,UITextFieldDelegate,GFAddressPickerDelegate,PickerViewResultDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSArray *titleArray;

@property(nonatomic,strong)NSArray *pickerArray;

@property (nonatomic,assign)NSInteger index;

@property (nonatomic,strong)STPickerSingle *picker;
@property (nonatomic,strong)GFAddressPicker *areaPicker;

///行业
@property (nonatomic,copy)NSString *industry;
@property (nonatomic,copy)NSString *profesion;
@property (nonatomic,copy)NSString *area;
@property (nonatomic,copy)NSString *workProperty;
@property (nonatomic,copy)NSString *studyNeed;
@property (nonatomic,copy)NSString *experince;


///选择的picker 对应的索引
//@property (nonatomic,copy)NSString *industry;
//@property (nonatomic,copy)NSString *profesion;
//@property (nonatomic,copy)NSString *area;
//@property (nonatomic,copy)NSString *workProperty;
//@property (nonatomic,copy)NSString *studyNeed;
//@property (nonatomic,copy)NSString *experince;

@property (nonatomic,copy)NSString *name;


@end

@implementation NSerchZhiweiViewController

#define cellIdentifier @"mineid"
#define SectionHeaderHeight  10.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 60.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMineCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        //        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

//企业招聘
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    
    self.index = 0;
    [self.navigationItem setItemWithTitle:@"职位搜索" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.titleArray = @[@"",@"行业",@"职业类型",@"所在地区",@"工作性质",@"学习要求",@"工作经验"];
    
    [self.view addSubview:self.tableView];
    [self addFooter];
    
    // Do any additional setup after loading the view.
}
-(void)addFooter {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 20, 300, 50);
    btn.centerX = footer.centerX;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"确认搜索" forState:UIControlStateNormal];
    [btn addTarget: self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
}
///点击搜索结果
- (void)tapAction:(UIButton *)btn
{
    
    NSDictionary *dict = @{@"empInfoTitle":self.name?:@"",@"empInfoIndustry":self.industry?:@"",@"empInfoPositionType":self.profesion?:@"",@"empInfoArea":self.area?:@"",@"empInfoProperty":self.workProperty?:@"",@"empInfoEducation":self.studyNeed?:@"",@"empInfoExperience":self.experince?:@"",@"size":@100,@"curPage":@1,};
    NPostionSearchResultVC *result = [NPostionSearchResultVC new];
    result.dict = dict;
    [self.navigationController pushViewController:result animated:YES];
}

#pragma mark - datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return  self.titleArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NMineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    cell.nameLabelLeftMargin.constant = 25.f;
    cell.nameLabel.text = self.titleArray[indexPath.section];
    cell.arrowImg.hidden = YES;
    cell.setLabel.hidden = false;
    cell.setLabel.text = @"请选择";
    
    if (indexPath.section==0) {
        cell.setLabel.hidden = YES;
        cell.titleTextField.hidden = false;
        cell.textFieldLeftMargin.constant = 25.f;
        cell.titleTextField.placeholder = @"关键词";
        cell.titleTextField.delegate = self;
        cell.titleTextField.tag = 1003;
        
    }
    
    switch (indexPath.section) {
        case 1:
        {
            if (self.industry) {
                cell.setLabel.text = self.industry;
            }
        }
            break;
        case 2:
        {
            if (self.profesion) {
                cell.setLabel.text = self.profesion;
            }
        }
            break;
        case 3:
        {
            if (self.area) {
                cell.setLabel.text = self.area;
            }
        }
            break;
        case 4:
        {
            if (self.workProperty) {
                cell.setLabel.text = self.workProperty;
            }
        }
            break;
        case 5:
        {
            if (self.studyNeed) {
                cell.setLabel.text = self.studyNeed;
            }
        }
            break;
        case 6:
        {
            if (self.experince) {
                cell.setLabel.text = self.experince;
            }
        }
            break;
            
        default:
            break;
    }
    return cell;
}


#pragma picker delegate

- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle{
    
    YYLog(@"%@",selectedTitle);
    
    switch (pickerSingle.tag) {
            
        case 101:
        {
            self.industry = selectedTitle;
        }
            break;
        case 102:
        {
            self.profesion = selectedTitle;
        }
            break;
        case 103:
        {
//            self.area = selectedTitle;
        }
            break;
        case 104:
        {
            self.workProperty = selectedTitle;
        }
            break;
        case 105:
        {
            self.studyNeed = selectedTitle;
        }
            break;
        case 106:
        {
            self.experince = selectedTitle;
        }
            break;
            
        default:
            break;
    }
    
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:self.index];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section!=0) {
        self.index = indexPath.section;
        [self addPickerWithIndexPath: indexPath];
        [self.view endEditing:YES];
    }
   
}

///添加picker
- (void)addPickerWithIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==3) {
        //地区选择器 省市区
//        self.areaPicker = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
//        [self.areaPicker updateAddressAtProvince:@"北京市" city:@"北京市" town:@"东城区"];
//        self.areaPicker.delegate = self;
//        self.areaPicker.font = [UIFont boldSystemFontOfSize:18];
//        [self.view addSubview:self.areaPicker];
        
        
        //地区选择器  省市
        PickerView *vi = [[PickerView alloc] init];
        vi.tag = 1000+indexPath.section;
        vi.delegate = self;
        vi.type = PickerViewTypeCity;
        [self.view addSubview:vi];
        
    }else{
        
        STPickerSingle *single = [[STPickerSingle alloc]init];
        [single setArrayData:[self getArrayWithIndexPath:indexPath]];
        single.tag = indexPath.section+100;
        [single setTitleUnit:@""];
        [single setDelegate:self];
        [single show];
    }
    
}

#pragma mark - 只有省市的地区选择器
-(void)pickerView:(UIView *)pickerView result:(NSString *)string{
    
    
    NSArray *array = [string componentsSeparatedByString:@"-"];
    string = [array lastObject];
    self.area = string;
    //刷新某一行
//    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:3];
//    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    
    ///两种刷新方式
    [self.tableView reloadData];
}


#pragma mark - 地区选择器delegate

- (void)GFAddressPickerCancleAction
{
    [self.areaPicker removeFromSuperview];
}

- (void)GFAddressPickerWithProvince:(NSString *)province
                               city:(NSString *)city area:(NSString *)area
{
    
    [self.areaPicker removeFromSuperview];
    self.area = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
//    self.reigion.text = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
//    [NGlobalModel shareInstance].region = self.reigion.text;
    //刷新某一行
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:3];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    YYLog(@"%@  %@  %@",province,city,area);
}


#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}


///获取picker 数据源
- (NSMutableArray *)getArrayWithIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 1:
        {
//            self.pickerArray = @[@"机械装备制造",@"电子信息",@"铝深加工",@"食品加工",@"生物制药",@"清洁能源",@"其他行业",@"不限",];
            ///为了写文档截图  临时修改的数据
            self.pickerArray = @[@"入库",@"入所",@"使用",@"平齐线",@"白阿线",@"长白线",@"哈大线",@"平齐线",];
            
        }
            break;
        case 2:
        {
            self.pickerArray = @[@"经济管理类",@"行政/人事/后勤类",@"财务/税务/审计类",@"生产制造/工厂管理类",@"市场/销售类",@"客服/技术服务类",@"计算机/互联网类",@"科研/咨询类",@"证券/金融/投资类",@"其他类",@"不限",];
        }
            break;
        case 3:
        {
//            self.pickerArray = @[@"北京市",@"天津市",@"上海市",@"重庆市",@"河北省",@"山西省",@"辽宁省",@"吉林省",@"黑龙江省",@"江苏省",@"安徽省",@"福建省",@"河北省",@"江西省",@"山东省",@"河南省",@"湖北省",@"湖南省",@"广东省",@"海南省",@"四川省",@"贵州省",@"云南省",@"陕西省",@"甘肃省",@"青海省",@"台湾省",@"内蒙古自治区",@"广西壮族自治区",@"西藏自治区",@"宁夏回族自治区",@"新疆维吾尔自治区",@"香港特别行政区",@"澳门特别行政区",];
        }
            break;
        case 4:
        {
            //工作性质
            self.pickerArray = @[@"全职",@"兼职",@"实习",@"不限",];
        }
            break;
        case 5:
        {
            //学习要求
            self.pickerArray = @[@"初中",@"高中",@"中专",@"大专",@"本科",@"硕士",@"博士",@"不限",];
            
        }
            break;
        case 6:
        {
            //工作经验
            
            self.pickerArray = @[@"1年以上",@"2年以上",@"3年以上",@"4年以上",@"5年以上",@"6年以上",@"7年以上",@"不限",];
        }
            break;
            
        default:
            break;
    }
    
    return (NSMutableArray *)self.pickerArray;
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag==1003) {
        self.name = textField.text;
    }
}

@end
